# release.mak: targets which simplify doing a one-step release.
#

release:
ifeq "$(origin tag)" "undefined"
	$(error Use: make tag=xx.xx.xx release)
else
	-mkdir -p RELEASE
	$(MAKE) RELEASE/dch-done.txt \
		RELEASE/release-tag-done.txt \
		RELEASE/debuild-done.txt \
		RELEASE/pypi-upload-done.txt
endif


RELEASE/dch-done.txt: msg = "This will update your changelog - type in new changes and update version $(tag).txt. ^C to cancel"
RELEASE/dch-done.txt:
	@read -p $(msg) x
	-@./build-tools/commit-popup
	dch -i
	touch $@

RELEASE/release-tag-done.txt: msg = "This will COMMIT $(PROJECT)/copyright.py and doc/release-notes/$(tag).txt and tag the release, and push changes. ^C to cancel"
RELEASE/release-tag-done.txt: doc/release-notes/$(tag).txt
	@read -p $(msg) x
	./build-tools/update-copyright $(tag)
	hg add doc/release-notes/$(tag).txt
	hg ci -m "releasing $(tag)" $(PROJECT)/copyright.py doc/release-notes/$(tag).txt debian/changelog
	@echo '!! This fetch might require a merge resolution.  Cancelling the merge will stop the release, but you probably wanted to anyway.'
	hg fetch
	hg tag $(tag)
	hg push
	touch $@

doc/release-notes/$(tag).txt: msg = "This will update your release notes - type in new release notes and update version $(tag). ^C to cancel"
doc/release-notes/$(tag).txt:
	@read -p $(msg) x
	cp doc/release-notes/template.txt doc/release-notes/$(tag).txt
	vim doc/release-notes/$(tag).txt

# remove old versions of $(PROJECT) to setup for release compliance testing
define purge-build-system
@read -p "This will REMOVE all system copies of $(PROJECT).  ^C to cancel" x
sudo apt-get remove python-$(PROJECT)
sudo rm -rf /usr/lib/python*/$(PROJECT)
sudo pip uninstall $(PROJECT)
endef

RELEASE/debuild-done.txt: PNAME := python-$(PROJECT)-"$(tag)"
RELEASE/debuild-done.txt:
	$(purge-build-system)
	-rm -rf RELEASE/$(PNAME) RELEASE/python-$(PROJECT)*
	hg archive -t files RELEASE/$(PNAME)
	cp -v Makefile RELEASE/
	$(MAKE) -C RELEASE PNAME=$(PNAME) debuild
	touch $@

define dver
export dver=`dpkg-parsechangelog | sed -n 's/Version: \(.*\)/\1/p'`
endef


debuild: msg = "This will build a debian package and then INSTALL it.  ^C to cancel"
debuild:
	@read -p $(msg) x
	tar cvfz $(PNAME).tar.gz $(PNAME)/
	cd $(PNAME) && debuild -sa
	sudo dpkg -i python-$(PROJECT)*.deb
	$(MAKE) tests
	cd $(PNAME) && debuild -S -sa

RELEASE/pypi-upload-done.txt: msg = "This will UPLOAD your sdist to pypi.  ^C to cancel"
RELEASE/pypi-upload-done.txt:
	$(purge-build-system)
	@read -p $(msg) x
	python setup.py sdist upload
	# change directories so pip doesn't pick up the local '$(PROJECT)'
	# directory
	cd RELEASE && sudo pip $(PROJECT) 
	python -c 'from $(PROJECT) import __version__ as v; assert v=="'$(tag)'"'
	$(MAKE) tests
	touch $@
